package edu.tomee.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.tomee.db.DatabaseConnection;
import edu.tomee.db.UserInfo;

/**
 * Servlet implementation class PwdChangeServlet
 */
@WebServlet("/pwdChange")
public class PwdChangeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	DatabaseConnection db = new DatabaseConnection();
	public PwdChangeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		String old_pwd = request.getParameter("T1");
		System.out.println("old_pwd "+old_pwd );
		String nw_pwd = request.getParameter("T2");
		HttpSession session = request.getSession();
		UserInfo ui = (UserInfo) session.getAttribute("rslt");
		int uid = ui.getUid();
		System.out.println("old paswrd matched with the database");
		int x = db.pwdChange(nw_pwd,uid,old_pwd);
		out.println("<html><head><title> Change Password</title>");
		request.getRequestDispatcher("/WEB-INF/jspf/header.jsp").include(request, response);
		out.println("</head><body class='metro'>");
		if(x>0){
			out.println("Password reset successfully");
		}
		else{
			out.println("Invalid entry");
		}
		out.println("</body></html>");
	}

}
